<?php

namespace app\cms\home;

use think\Db;
use think\Request;


class Index extends Common
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public function index()
    {
        //推荐阅读
        $data = Db::name('cms_document')
            ->where('cid',2)
            ->order('id desc')
            ->paginate(5);
        $dataArr = $data->items();
        foreach ($dataArr as $key => $v) {
            $dataArr[$key]['content'] = Db::name('cms_document_article')
                ->where(['aid' => $v['id']])
                ->find();
        }
        $this->assign('data', $dataArr);
        //我们的故事
        $data2 = Db::name('cms_document')
            ->where('cid',8)
            ->order('id desc')
            ->paginate(5);
        $dataArr2 = $data2->items();
        foreach ($dataArr2 as $key => $v) {
            $dataArr2[$key]['content'] = Db::name('cms_document_article')
                ->where(['aid' => $v['id']])
                ->find();
        }
        $this->assign('data2', $dataArr2);
        //活动预告
        $data3 = Db::name('cms_document')
            ->where('cid',7)
            ->order('id desc')
            ->paginate(5);
        $dataArr3 = $data3->items();
        foreach ($dataArr3 as $key => $v) {
            $dataArr3[$key]['content'] = Db::name('cms_document_article')
                ->where(['aid' => $v['id']])
                ->find();
        }
        $this->assign('data3', $dataArr3);
        //雨情心情
        $data4 = Db::name('cms_document')
            ->where('cid',5)
            ->order('id desc')
            ->paginate(5);
        $dataArr4 = $data4->items();
        foreach ($dataArr4 as $key => $v) {
            $dataArr4[$key]['content'] = Db::name('cms_document_article')
                ->where(['aid' => $v['id']])
                ->find();
        }
        $this->assign('data4', $dataArr4);

        $this->assign('menu', 'index');

        return $this->fetch();
    }

    public function pages($id=0,$keyword=''){
        if($keyword==''){
            $data = Db::name('cms_document')
                ->where('cid','=',$id)
                ->order('id desc')
                ->paginate(15,true);
        }else{
            $data = Db::name('cms_document')
                ->where('title','like','%'.$keyword.'%')
                ->order('id desc')
                ->paginate(15,true);
        }
        $this->assign('keyword',$keyword);
        $page = $data->render();
        $this->assign('page',$page);
        $dataArr = $data->items();
        foreach ($dataArr as $key => $v) {
            $dataArr[$key]['content'] = Db::name('cms_document_article')
                ->where(['aid' => $v['id']])
                ->find();
        }
        if($id==0){$menu='search';}
        if($id==2){$menu='tuijianyuedu';}
        if($id==5){$menu='yuqingxinwen';}
        if($id==6){$menu='guizhangzhidu';}
        if($id==7){$menu='yuqingkafeiguan';}
        if($id==8){$menu='womendegushi';}
        $this->assign('menu', $menu);

        $this->assign('data', $dataArr);
        $this->assign('cidname',Db::name('cms_column')->where('id',$id)->value('name'));

        return $this->fetch();
    }
}